﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using TomShane.Neoforce.Controls;

namespace Orion.Lib
{
    public class LevelListScreen : GameScreenBase
    {
        public LevelListScreen()
            : base()
        { }

        public override void Initialize()
        {
            base.Initialize();

        }

        public override void LoadContent()
        {
            if (!isLoaded)
            {
                base.LoadContent();

                MyGame.Instance.IsMouseVisible = true;


                CreateUI();
            }
        }
        
        Window window;
        List<Button> buttons = new List<Button>();
        private void CreateUI()
        {
            // Create and setup Window control.
            window = new Window(MyGame.UIManager);
            window.Init();
            window.Text = "Select Level";
            window.Width = 270;
            window.Height = 300;
            window.Center();
            window.Resizable = false;
            window.CloseButtonVisible = false;
            window.Visible = true;
            window.Movable = false;

            var vert = new ScrollBar(MyGame.UIManager, Orientation.Vertical);
            vert.Init();
            vert.Parent = window;
            vert.Top = 0;
            vert.Left = window.ClientWidth - vert.Width;
            vert.Height = window.ClientHeight - vert.Width;
            vert.Value = 0;
            vert.Anchor = Anchors.Top | Anchors.Right | Anchors.Bottom;
            vert.ValueChanged += new TomShane.Neoforce.Controls.EventHandler(vert_ValueChanged);
            int totalheight = 0; 
            foreach (var level in LevelManager.LevelList)
            {
                var button = new Button(MyGame.UIManager);
                button.Init();
                button.Text = level.Name;
                button.Width = 200;
                button.Height = 24;
                button.Tag = level;
                button.Click += new TomShane.Neoforce.Controls.EventHandler(button_Click);
                button.Left = (window.ClientWidth / 2 - button.Width / 2);
                button.Top = (10) + button.Height + 29 * LevelManager.LevelList.IndexOf(level);
                button.Anchor = Anchors.Horizontal | Anchors.Vertical;
                button.Parent = window;
                totalheight =  button.Top -button.Height ;
                buttons.Add(button);
            }

            vert.Range = totalheight;
            vert.PageSize = 50;
            MyGame.UIManager.Add(window);
        }

        void vert_ValueChanged(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            var send = (ScrollBar)sender;
            foreach (var button in buttons)
            {
                button.Top = (10) + button.Height + 29 * buttons.IndexOf(button) - send.Value;
            }
        }

        void button_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            var button = (Button)sender;

            LevelManager.LoadLevel((LevelDescriptor)button.Tag, null, null, null);
            window.Close();
        }


        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);
        }
    }
}
